﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cMessageBox
{
    public partial class CustomBox : Form
    {
        #region Constructor
        public CustomBox()
        {
            IconMessage = MessageIcon.MESSAGE2;
            Theme = ThemeColor.Blue;
            InitializeComponent();
        }
        public CustomBox(string message)
        {
            TextMessage = message;
            IconMessage = MessageIcon.MESSAGE2;
            Theme = ThemeColor.Blue;
            InitializeComponent();
        }
        public CustomBox(string message, MessageIcon icon)
        {
            TextMessage = message;
            IconMessage = icon;
            InitializeComponent();
        }
        public CustomBox(string message, MessageIcon icon, TypeButton type)
        {
            TextMessage = message;
            IconMessage = icon;
            buttonType = type;
            InitializeComponent();
        }
        public CustomBox(string message, MessageIcon icon, TypeButton type, ThemeColor theme)
        {
            TextMessage = message;
            IconMessage = icon;
            Theme = theme;
            buttonType = type;
            InitializeComponent();
        }
        #endregion

        #region property

        private MessageIcon iconMessage;
        private string textMessage = "";
        private DialogResult result;
        private ThemeColor theme;
        private string titleText = "Message";
        private string fontName = "Microsoft Sans Serif";
        private TypeButton buttonType;

        private int WindowWidth
        {
            get { return this.Width; }
        }

        public TypeButton ButtonType
        {
            get { return buttonType; }
            set { buttonType = value; }
        }

        public ThemeColor Theme
        {
            get { return theme; }
            set { theme = value; }
        }
        /// <summary>
        /// Get or set title of message box.
        /// </summary>
        public string TitleText
        {
            get { return titleText; }
            set { titleText = value; }
        }
        /// <summary>
        /// Get dialog result, can't set. Read only.
        /// </summary>
        public DialogResult Result
        {
            get { return result; }
            set { result = value; }
        }
        /// <summary>
        /// Get or set display text.
        /// </summary>
        public string TextMessage
        {
            get { return textMessage; }
            set { textMessage = value; }
        }
        /// <summary>
        /// Use custom message box icon. Icon "Message" is default.
        /// </summary>
        public MessageIcon IconMessage
        {
            get { return iconMessage; }
            set { iconMessage = value; }
        }

        #endregion

        #region Button
        private void ButtonYes(System.Drawing.Point point)
        {
            Button buttonYes = new Button();
            buttonYes.Font = new System.Drawing.Font(fontName, 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonYes.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            buttonYes.ImageIndex = 1;
            buttonYes.ImageList = this.buttonList;
            buttonYes.Location = point;
            buttonYes.Name = "btYes";
            buttonYes.Size = new System.Drawing.Size(120, 43);
            buttonYes.TabIndex = 0;
            buttonYes.Text = "Yes";
            buttonYes.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            buttonYes.UseVisualStyleBackColor = true;
            buttonYes.Click += new EventHandler(buttonYes_Click);
            panel1.Controls.Add(buttonYes);
        }

        private void ButtonNo(System.Drawing.Point point)
        {
            Button buttonNo = new Button();
            buttonNo.Font = new System.Drawing.Font(fontName, 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonNo.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            buttonNo.ImageIndex = 2;
            buttonNo.ImageList = this.buttonList;
            buttonNo.Location = point;
            buttonNo.Name = "btNo";
            buttonNo.Size = new System.Drawing.Size(120, 43);
            buttonNo.TabIndex = 0;
            buttonNo.Text = "No";
            buttonNo.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            buttonNo.UseVisualStyleBackColor = true;
            buttonNo.Click += new EventHandler(buttonNo_Click);
            panel1.Controls.Add(buttonNo);
        }

        private void ButtonOK(System.Drawing.Point point)
        {
            Button buttonOK = new Button();
            buttonOK.Font = new System.Drawing.Font(fontName, 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonOK.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            buttonOK.ImageIndex = 1;
            buttonOK.ImageList = this.buttonList;
            buttonOK.Location = point;
            buttonOK.Name = "btOK";
            buttonOK.Size = new System.Drawing.Size(120, 43);
            buttonOK.TabIndex = 0;
            buttonOK.Text = "OK";
            buttonOK.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            buttonOK.UseVisualStyleBackColor = true;
            buttonOK.Click += new EventHandler(buttonOK_Click);
            panel1.Controls.Add(buttonOK);
        }

        private void ButtonCancel(System.Drawing.Point point)
        {
            Button buttonCancel = new Button();
            buttonCancel.Font = new System.Drawing.Font(fontName, 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            buttonCancel.ImageIndex = 3;
            buttonCancel.ImageList = this.buttonList;
            buttonCancel.Location = point;
            buttonCancel.Name = "btcancel";
            buttonCancel.Size = new System.Drawing.Size(120, 43);
            buttonCancel.TabIndex = 0;
            buttonCancel.Text = "Cancel";
            buttonCancel.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            buttonCancel.UseVisualStyleBackColor = true;
            buttonCancel.Click += new EventHandler(buttonCancel_Click);
            panel1.Controls.Add(buttonCancel);
        }
        #endregion

        #region Button event
        void buttonCancel_Click(object sender, EventArgs e)
        {
            Result = DialogResult.Cancel;
            this.Close();
            //throw new NotImplementedException();
        }

        void buttonOK_Click(object sender, EventArgs e)
        {
            Result = DialogResult.OK;
            this.Close();
            //throw new NotImplementedException();
        }

        void buttonNo_Click(object sender, EventArgs e)
        {
            Result = DialogResult.No;
            this.Close();
            //throw new NotImplementedException();
        }

        void buttonYes_Click(object sender, EventArgs e)
        {
            Result = DialogResult.Yes;
            this.Close();
            //throw new NotImplementedException();
        }
        #endregion

        #region enum
        public enum MessageIcon
        {
            INFO_BLUE = 0
            ,
            INFO_ORANGE = 1
                ,
            ALERT_BLUE = 2
                ,
            ALERT_ORANGE = 3
                ,
            CANCEL = 4
                ,
            CANCEL2 = 5
                ,
            MESSAGE = 6
                ,
            MESSAGE2 = 7
                ,
            PASS = 8
                ,
            FLAG = 9
                ,
            CONNECT_PROBLEM = 10
                , ERROR = 11
        }

        public enum TypeButton
        {
            YES
                ,
            YESNO
                ,
            OK
                , OKCANCEL
        }

        public enum ThemeColor
        {
            Brown = 0
            , Blue = 1
        }
        #endregion

        #region method
        private void Form1_Load(object sender, EventArgs e)
        {
            ChangeTheme(Theme);
            DrawButton();
            label1.Text = TitleText;
            label2.Text = TextMessage;
            pictureBox1.Image = imgList.Images[(int)IconMessage];
        }


        private void pictureBox2_Click(object sender, EventArgs e)
        {
            result = DialogResult.Cancel;
            this.Close();
        }
        private void ChangeTheme(ThemeColor color)
        {
            tableLayoutPanel1.BackgroundImage = bgList.Images[(int)color];
        }

        private void DrawButton()
        {
            switch (ButtonType)
            {
                case TypeButton.YES :
                    Point yes = new Point();
                    yes.X = (int)Math.Round(WindowWidth / 2.0)-60;//90/2=45, 120/2=60
                    yes.Y = 0;
                    ButtonYes(yes);
                    break;
                case TypeButton.YESNO:
                    //total length = 120+120 , plus gap 10 total 120+120+10=250
                    //first location of 2 button is center screen - 120 - 5 
                    Point yes1 = new Point();
                    yes1.X = (int)Math.Round(WindowWidth / 2.0) - 125;
                    yes1.Y = 0;
                    ButtonYes(yes1);
                    //second button start location at center+5, because gap is 10
                    Point no1 = new Point();
                    no1.X = (int)Math.Round(WindowWidth / 2.0) + 5;
                    no1.Y = 0;
                    ButtonNo(no1);
                    break;
                case TypeButton.OK:
                    Point ok = new Point();
                    ok.X = (int)Math.Round(WindowWidth / 2.0) - 60;
                    ok.Y = 0;
                    ButtonOK(ok);
                    break;
                case TypeButton.OKCANCEL:
                    Point ok1 = new Point();
                    ok1.X = (int)Math.Round(WindowWidth / 2.0) - 125;
                    ok1.Y = 0;
                    ButtonOK(ok1);
                    Point cancel = new Point();
                    cancel.X = (int)Math.Round(WindowWidth / 2.0) + 5;
                    cancel.Y = 0;
                    ButtonCancel(cancel);
                    break;
                default :
                    break;
            }

        }
        #endregion
    }
}
